package com.upup.train_management.controller;

import com.upup.train_management.entity.Schedule;
import com.upup.train_management.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/schedules")
public class ScheduleController {

    private final ScheduleService scheduleService;

    @Autowired
    public ScheduleController(ScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    @GetMapping
    public ResponseEntity<List<Schedule>> getAllSchedules() {
        return ResponseEntity.ok(scheduleService.getAllSchedules());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Schedule> getScheduleById(@PathVariable Long id) {
        Schedule schedule = scheduleService.getScheduleById(id);
        if (schedule != null) {
            return ResponseEntity.ok(schedule);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/train/{trainId}")
    public ResponseEntity<List<Schedule>> getSchedulesByTrainId(@PathVariable Long trainId) {
        return ResponseEntity.ok(scheduleService.findSchedulesByTrainId(trainId));
    }

    @GetMapping("/route/{routeId}")
    public ResponseEntity<List<Schedule>> getSchedulesByRouteId(@PathVariable Long routeId) {
        return ResponseEntity.ok(scheduleService.findSchedulesByRouteId(routeId));
    }

    @GetMapping("/date/{date}")
    public ResponseEntity<List<Schedule>> getSchedulesByDate(
            @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        return ResponseEntity.ok(scheduleService.findSchedulesByDate(date));
    }

    @GetMapping("/date-range")
    public ResponseEntity<List<Schedule>> getSchedulesByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return ResponseEntity.ok(scheduleService.findSchedulesByDateRange(startDate, endDate));
    }

    @GetMapping("/active")
    public ResponseEntity<List<Schedule>> getActiveSchedules() {
        return ResponseEntity.ok(scheduleService.getActiveSchedules());
    }

    @GetMapping("/search")
    public ResponseEntity<List<Schedule>> searchSchedules(
            @RequestParam Long startStationId,
            @RequestParam Long endStationId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        return ResponseEntity.ok(scheduleService.searchSchedules(startStationId, endStationId, date));
    }

    @GetMapping("/{id}/available-seats")
    public ResponseEntity<Integer> getAvailableSeats(@PathVariable Long id) {
        return ResponseEntity.ok(scheduleService.getAvailableSeats(id));
    }

    @PostMapping
    public ResponseEntity<Schedule> createSchedule(@RequestBody Schedule schedule) {
        return ResponseEntity.ok(scheduleService.saveSchedule(schedule));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Schedule> updateSchedule(@PathVariable Long id, @RequestBody Schedule schedule) {
        Schedule existingSchedule = scheduleService.getScheduleById(id);
        if (existingSchedule == null) {
            return ResponseEntity.notFound().build();
        }
        schedule.setId(id);
        return ResponseEntity.ok(scheduleService.saveSchedule(schedule));
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<Void> updateScheduleStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        boolean updated = scheduleService.updateScheduleStatus(id, status);
        if (updated) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSchedule(@PathVariable Long id) {
        Schedule existingSchedule = scheduleService.getScheduleById(id);
        if (existingSchedule == null) {
            return ResponseEntity.notFound().build();
        }
        scheduleService.deleteSchedule(id);
        return ResponseEntity.noContent().build();
    }
}